﻿//ajax 查询Employee表
function employeesQuery() {
    var employeeID = $("#employeeIDInput").val();
    SetWait($("body", parent.document), true);
    $.get("../BodyGUI/GetSingleTable",
    { objectID: employeeID, objectType: 'employees' },
    function (data, status) {
        if (status == "success") {
            var dataJson = eval("(" + data + ")");
            buildEmployeesGrid(dataJson);
        }
        SetWait($("body", parent.document), false);
    });
}

//ajax 查询Department表
function departmentsQuery() {
    var departmentID = $("#departmentIDInput").val();
    SetWait($("body", parent.document), true);
    $.get("../BodyGUI/GetSingleTable",
    { objectID: departmentID, objectType: 'departments' },
    function (data, status) {
        if (status == "success") {
            var dataJson = eval("(" + data + ")");
            buildDepartmentsGrid(dataJson);
        }
        SetWait($("body", parent.document), false);
    });
}

//ajax 查询Employee表和Department表
function unionQuery() {
    SetWait($("body", parent.document), true);
    $.get("../BodyGUI/GetSingleTable",
    { objectID: '', objectType: 'departments' },
    function (data, status) {
        if (status == "success") {
            var dataJson = eval("(" + data + ")");
            buildUnionConditionQuery(dataJson);
        }
        SetWait($("body", parent.document), false);
    });

    SetWait($("body", parent.document), true);
    $.get("../BodyGUI/GetSingleTable",
    { objectID: '', objectType: 'employees' },
    function (data, status) {
        if (status == "success") {
            var dataJson = eval("(" + data + ")");
            buildUnionContentQuery(dataJson);
        }
        SetWait($("body", parent.document), false);
    });
}

//创建Employees的Grid
function buildEmployeesGrid(dataJson) {
    var singleGrid = $("#dataGrid");

    var obj = { title: "员工列表", resizable: true, roundCorners: false,
        width: "100%", height: "100%", hoverMode: "cell", editable: false, draggable: true
    };
    obj.colModel = [{ title: "employeeID", width: 100, dataType: "integer", dataIndx: "employeeID" },
                       { title: "onDepartment", width: 200, dataType: "integer", dataIndx: "onDepartment" },
                       { title: "name", width: 150, dataType: "string", align: "right", dataIndx: "name" },
                       { title: "introduction", width: 150, dataType: "string", align: "right", dataIndx: "introduction"}];
    obj.dataModel = {
        data: dataJson,
        sortIndx: "employeeID",
        sortDir: "up"
    };

    var grid = singleGrid.pqGrid(obj);

}

//创建Departments的Grid
function buildDepartmentsGrid(dataJson) {
    var singleGrid = $("#dataGrid");

    var obj = { title: "部门列表", resizable: true, roundCorners: false,
        width: "100%", height: "100%"
    };
    obj.colModel = [{ title: "departmentID", width: 100, dataType: "integer", dataIndx: "departmentID" },
                       { title: "departmentName", width: 200, dataType: "string", dataIndx: "departmentName" },
                       { title: "departmentTotal", width: 150, dataType: "string", align: "right", dataIndx: "departmentTotal" },
                       { title: "departmentIntroduction", width: 150, dataType: "string", align: "right", dataIndx: "departmentIntroduction"}];
    obj.dataModel = {
        data: dataJson,
        sortIndx: "departmentID",
        sortDir: "up"
    };

    var grid = singleGrid.pqGrid(obj);

}

//创建联动查询的Employees Grid
function buildUnionContentQuery(dataJson) {
    employeesJsonData = dataJson;
    var source = { datatype: "json",
        datafields: [
                        { name: 'employeeID' },
                        { name: 'onDepartment' },
                        { name: 'name' },
                        { name: 'introduction' }
                    ],
        id: 'employeeID',
        localdata: dataJson
    };
    var dataAdapter = new $.jqx.dataAdapter(source);
    var theme = "";

    $("#employeesGrid").jqxGrid(
            {
                localization: getLocalization(),
                selectionmode: '•multiplerows',
                width: '100%',
                height: '100%',
                pageable: true,
                sortable: true,
                source: dataAdapter,
                theme: theme,
                columnsresize: true,
                columns: [
                  { text: '员工号', datafield: 'employeeID', width: 250 },
                  { text: '在部门', datafield: 'onDepartment', width: 250 },
                  { text: '姓名', datafield: 'name', width: 250 },
                  { text: '简介', datafield: 'introduction', width: 250}],
                toolbarheight: 34,
                showtoolbar: true,
                rendertoolbar: function (toolbar) {
                    var container = $("<div style='margin: 5px;'></div>");
                    var span = $("<span style='float: left; margin-top: 5px; margin-right: 4px;'>员工列表: </span>");
                    //var input = $("<input class='jqx-input jqx-widget-content jqx-rc-all' id='searchField' type='text' style='clear:left;height: 23px; float: left; width: 223px;' />");
                    toolbar.append(container);
                    container.append(span);
                    //container.append(input);

                }

            });


}

//创建联动查询的Departments Grid
function buildUnionConditionQuery(dataJson) {
    // Create jqxExpander
    $('#jqxExpander').jqxExpander({ showArrow: false, toggleMode: 'none', width: '100%', height: '100%', theme: theme });
    // Create jqxTree
    var source = { datatype: "json",
        datafields: [
                        { name: 'departmentID' },
                        { name: 'value', map: 'departmentID' },
                        { name: 'departmentName' },
                        { name: 'departmentTotal' },
                        { name: 'departmentIntroduction' },
                         { name: 'parentDepartment' }
                    ],
        id: 'departmentID',
        localdata: dataJson
    };

    var dataAdapter = new $.jqx.dataAdapter(source);
    dataAdapter.dataBind();
    var records = dataAdapter.getRecordsHierarchy('departmentID', 'parentDepartment', 'items', [{ name: 'departmentName', map: 'label'}]);
    var theme = "";
    $('#jqxTree').jqxTree({ source: records, width: '100%', height: '100%', theme: theme });
    $('#jqxTree').jqxTree('expandAll');

    $('#jqxTree').on('select', function (event) {
        var item = $('#jqxTree').jqxTree('getItem', event.args.element);
        conditionTreeSelected(item.value);
    });
}

//Departments树选择时的处理
function conditionTreeSelected(selectedDeparment) {
    $("#employeesGrid").jqxGrid('clearselection');
    $(employeesJsonData).each(function (index, element) {
        if (element.onDepartment == selectedDeparment)
        { $("#employeesGrid").jqxGrid('selectrow', index); }
    });
}